<?php

/**
 * @author anhtung
 * @copyright 2013
 */

class document extends CI_Controller{
    
    protected $mahoso;
    
    function __construct(){
        parent::__construct();
        $this->load->helper('url');
        $this->load->library(array('form_validation','session'));
        $this->load->library('userlayout');
        $this->userlayout->setLayout("user/layout");
        
        $userSession = $this->session->userdata('user');
        if($userSession['username'] == false){
            redirect(base_url('/index/login?returnUrl=').urlencode(RETURN_URL));
        }
        $this->mahoso = $userSession['MaHoSo'];
        $this->load->model('user/document_model');
    }
    function createdocument(){
        $title = "Tạo Mẫu Báo Cáo";
        $currTemp = array('&nbsp;&nbsp;<a href= "'.base_url('nhanvien/document').'">Mẫu Báo Cáo</a> &raquo;Tạo Mẫu Báo Cáo');
        $current['current'] = $currTemp;
        $data = array();
        if(isset($_GET['error']) && ($_GET['error'] != '')){
            $data['error'] = $_GET['error'];    
        }
        $this->userlayout->view("user/createdocument",$data ,$title, $current);
        
    }
    
    function docreate(){
        $data = array();
        if(isset($this->mahoso) && !empty($this->mahoso)){
            $data['MaHoSo'] = $this->mahoso;
        }
        if(isset($_POST['title']) && !empty($_POST['title'])){
            $data['TieuDe'] = $_POST['title'];
        }
        if(isset($_POST['descrip']) && !empty($_POST['descrip'])){
            $data['Mota'] = $_POST['descrip'];
        }
        $data['Ngay'] = date('Y-m-d',time());
        
        if(isset($_FILES['detail']) && ($_FILES['detail']['error'] == 0)){
            $uploaddir = APPROOT.'resources/documenttemplate/';
            $path = $_FILES['detail']['name'];
            $ext = pathinfo($path, PATHINFO_EXTENSION);
            $time = time();
            $file_name = basename($_FILES['detail']['name']);
            $file_path = $uploaddir.$file_name;
            if(file_exists($file_path)){
                $file_name = $time."-".basename($_FILES['detail']['name']);
                $file_path = $uploaddir.$file_name;
            }
        
            if(move_uploaded_file($_FILES['detail']['tmp_name'],$file_path)){
                $data['URL'] = $file_name;
                if($this->document_model->create($data)){
                    //redirect('nhanvien/document/abc');
                }else{
                    $error = "Lỗi cơ sở dư liệu vui lòng thử lại";
                    redirect('nhanvien/document/createdocument?error='.$error);
                }
            }else{
                $error = "Upload file thất bại vui lòng thử lại";
                redirect('nhanvien/document/createdocument?error='.$error);
            }
        }else{
            $error = "Chưa chọn file";
            redirect('nhanvien/document/createdocument?error='.$error);
        }
        
        redirect('nhanvien/document');
    }
    
    function index(){
        $sort = 'Ngay';
        $this->load->library('pagination'); 
        $this->load->helper('url'); 
        $config['next_link'] = 'Next »'; 
        $config['prev_link'] = '« Prev'; 
        $config['num_links']	= 10;
        $config['base_url'] = base_url('nhanvien/document'); // xác định trang phân trang 
        $config['total_rows'] = $this->document_model->count_all(); // xác định tổng số record 
        $config['per_page'] = 15; // xác định số record ở mỗi trang 
        $config['uri_segment'] = 4; // xác định segment chứa page number 
        $this->pagination->initialize($config); 
        $dataDocument = $this->document_model->list_all($config['per_page'],$this->uri->segment(4), $sort);
        $this->load->model('user/hoso_model');
        $data["links"] = $this->pagination->create_links();
        $data['data'] = $dataDocument;
        $title = "Mẫu báo cáo";
        $currTemp = array('&nbsp;&nbsp;Mẫu báo cáo');
        $current['current'] = $currTemp;
        $this->userlayout->view("user/documentIndex",$data,$title, $current ); 
         
    }
    
    function download($url){
        if($url != null){
            $download_path = $uploaddir = APPROOT.'resources/documenttemplate/';
            $url = urldecode($url);
            $file = $download_path.$url; 
            if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist."); 
 
            $type = filetype($file);  
            $today = date("F j, Y, g:i a"); 

            $time = time(); 
            header("Content-type: $type"); 
            header("Content-Disposition: attachment;filename=$filename"); 
            header("Content-Transfer-Encoding: binary"); 
            header('Pragma: no-cache'); 
            header('Expires: 0'); 
            set_time_limit(0); 
            readfile($file);  
        }
    }
    
}

?>